//
//  NewsViewController.m
//  555彩票
//
//  Created by zhoubo on 2017/7/4.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "NewsViewController.h"

#import "ChannelLabel.h"
#import "Channel.h"

@interface NewsViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *channelView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;



/**
 *  控制器缓存池
 */
@property (nonatomic, strong) NSMutableDictionary *controllerCache;

/**
 *  布局参数
 */
@property (nonatomic, weak) IBOutlet UICollectionViewFlowLayout *flowLayout;
/**
 *  新闻频道数组
 */
@property (nonatomic, strong) NSArray *channels;

@property (nonatomic,assign) NSInteger  currentIndex;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

// 在 iOS 7.0 之后，如果视图控制器中有 UINavigationController + UIScrollView的组合，会默认调整 UIScrollView 的边距，从而达到穿透 Nav 的效果
/**
 *  添加频道标签
 */
- (void)setupChannel {
    // 设置不要自动调整UIScrollView的内边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    // 获得channelView的高度
    CGFloat channelH = self.channelView.frame.size.height;
    // 频道标签之间的间隙
    CGFloat marginX = 8;
    CGFloat lableX = marginX;
    for (Channel *channel in self.channels) {
        // 创建 频道标签
        ChannelLabel *channelLabel = [ChannelLabel labelWithTitle:channel.name];
        // 设置x和y值
        channelLabel.frame = CGRectMake(lableX, 0, channelLabel.frame.size.width, channelH);
        
        // 设置tag标签
        //        channelLabel.tag =
        
        __weak typeof(self) weakVc = self;
        __weak typeof(channelLabel) weakChannelLabel = channelLabel;
        // 设置block
        [channelLabel setSelectedlabel:^{
            // 根据currentIndex获得当前的选中的频道标签
            ChannelLabel *currentLabel = weakVc.channelView.subviews[weakVc.currentIndex];
            currentLabel.scale = 0;
            weakChannelLabel.scale = 1;
            // 重新设置currentIndex
            weakVc.currentIndex = [weakVc.channelView.subviews indexOfObject:weakChannelLabel];
            
            // 滚动cell到指定的位置
            [weakVc.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:weakVc.currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
            
            // 获得频道标签中心点的x值
            CGFloat centerX = weakChannelLabel.center.x;
            // 计算屏幕宽度的一半
            CGFloat halfOfScreen = [UIScreen mainScreen].bounds.size.width * 0.5;
            // 获得scrollView最大滚动范围
            CGFloat maxOffsetX = weakVc.channelView.contentSize.width - [UIScreen mainScreen].bounds.size.width ;
            // 计算偏移量
            CGFloat offsetX = centerX - halfOfScreen;
            if (offsetX < 0) {
                offsetX = 0;
            } else if (offsetX > maxOffsetX) {
                offsetX = maxOffsetX;
            }
            [weakVc.channelView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
        }];
        // 将频道标签添加channelView上
        [self.channelView addSubview:channelLabel];
        
        lableX += channelLabel.frame.size.width;
    }
    
    // 设置滚动范围
    self.channelView.contentSize = CGSizeMake(lableX + marginX, 0);
    self.channelView.showsHorizontalScrollIndicator = NO;
    
    // 设置默认
    self.currentIndex = 0;
    ChannelLabel *currentLabel = self.channelView.subviews[self.currentIndex];
    currentLabel.scale = 1;
}

/**
 *  当控制的view布局好之后调用
 */
- (void)viewDidLayoutSubviews {
    // 一定要记得调用父类
    [super viewDidLayoutSubviews];
    // 设置布局属性
    [self setupFlowLayout];
}


/**
 *  设置布局属性
 */
- (void)setupFlowLayout {
    // 设置item的尺寸
    self.flowLayout.itemSize = self.collectionView.bounds.size;
    NSLog(@"%@",NSStringFromCGSize(self.collectionView.bounds.size));
    // 设置item之间的间隙
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.minimumLineSpacing = 0;
    
    // 设置方向
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 设置分页效果
    self.collectionView.pagingEnabled = YES;
    // 设置隐藏水平滚动条
    self.collectionView.showsHorizontalScrollIndicator = NO;
    // 禁止弹簧效果
    self.collectionView.bounces = NO;
}

#pragma mark - UIScrollView 代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 根据currentIndex获得频道标签
    ChannelLabel *currentLabel = self.channelView.subviews[self.currentIndex];
    // 下个频道标签
    ChannelLabel *nextLabel = nil;
    // 获得当前可视的cell对应的item
    NSArray *indexPaths = [self.collectionView indexPathsForVisibleItems];
    // 查找下一个频道标签
    for(NSIndexPath *indexPath in indexPaths) {
        if(indexPath.item != self.currentIndex) {
            nextLabel = self.channelView.subviews[indexPath.item];
            break;
        }
    }
    
    // 比例 （0到1）
    // 下一个频道标签的缩放比例
    CGFloat nextLabelScale  = ABS((CGFloat)scrollView.contentOffset.x / scrollView.bounds.size.width - self.currentIndex);
    // 当前频道标签的缩放比例
    CGFloat currentLabelScale = 1 - nextLabelScale;
    
    currentLabel.scale = currentLabelScale;
    nextLabel.scale = nextLabelScale;
    
    NSLog(@"%f#####%f",currentLabelScale,nextLabelScale);
    //    NSLog(@"从 %@ 到 %@",currentLabel.text,nextLabel.text);
    //    NSLog(@"indexPaths = %@",indexPaths);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 重置 currentIndex
    self.currentIndex =  scrollView.contentOffset.x / scrollView.bounds.size.width;
    //    NSLog(@"self.currentIndex  = %zd",self.currentIndex );
}

#pragma mark - collectionView 数据源方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.channels.count;
}


@end
